function updateItem(itemId) {
    let count = $("#goodsCount" + itemId).val();
    if (count < 1) {
        count = 1;
        $("#goodsCount" + itemId).val(count);
    } else if (count > 5) {
        count = 5;
        $("#goodsCount" + itemId).val(count);
    }

    $.ajax({
        url: ctx + "/shopping/cart/updateItem",
        method: "post",
        data: {
            itemId: itemId,
            count: count
        },
        success: function(resp) {
            if (resp.code === "200") {
                location.reload();
            } else {
                Swal.fire({
                    text: resp.message || "更新失败",
                    icon: "error",
                    iconColor: "#f05b72",
                });
            }
        },
        error: function() {
            Swal.fire({
                text: "网络错误，更新失败",
                icon: "error",
                iconColor: "#f05b72",
            });
        }
    });
}

$(function () {
    $(".deleteItem").click(function() {
        let itemId = $(this).data("id");
        Swal.fire({
            title: "确认弹框",
            text: "确认要删除数据吗?",
            icon: "warning",iconColor:"#dea32c",
            showCancelButton: true,
            confirmButtonText: '确认',
            cancelButtonText: '取消'
        }).then((flag) => {
            if (flag.value){
                $.ajax({
                    url:ctx+"/shopping/deleteCart",
                    method:"post",
                    data:{
                        itemId:itemId
                    },
                    success: function (resp) {
                        if (resp.code === "200") {
                            window.location.reload();
                        } else {
                            Swal.fire({
                                text: "操作失败",
                                icon: "error",iconColor:"#f05b72",
                            });
                        }
                    },
                    error: function () {
                        Swal.fire({
                            text: "操作失败",
                            icon: "error",iconColor:"#f05b72",
                        });
                    }
                });
            }
        });
    });

    $("#pay").click(function(){
        location.href = ctx + "/shopping/cart/settle";
    });
});